﻿using System.Linq;
using Missbot.Common;
using Missbot.Core.Base;
using Missbot.Core.Cache;
using Dolany.UtilityTool;
using JetBrains.Annotations;
using Missbot.AIModule.Base;
using Missbot.AIModule.Gaming.Gift;
using Missbot.AIModule.OnlineStore;
using Missbot.AIModule.Pet;
using Missbot.Model.MsgInfos;

namespace Missbot.AIModule.Sys
{
    [UsedImplicitly]
    public class SuperAdminMod : IAIModule
    {
        public override string Name => "超管";
        public override AIPriority Priority => AIPriority.SuperHigh;
        public override string ParentTag => "系统功能";

        #region Svcs

        public WaiterSvc WaiterSvc { get; set; }
        public AIModuleMgr AIModuleMgr { get; set; }
        public PicReviewSvc PicReviewSvc { get; set; }
        public DataRefreshSvc DataRefreshSvc { get; set; }
        public HonorSvc HonorSvc { get; set; }
        public GiftSvc GiftSvc { get; set; }
        
        #endregion

        [ModuleEnter(Command = "功能奖励", Description = "奖励某个人某个功能若个使用次数（当日有效）", SyntaxHint = "[命令名] [@用户名] [奖励个数]", AuthLvl = AuthorityLevel.Developer)]
        public bool FunctionBonus(InboxSession session, string command, InstanceUser aimUser, int count)
        {
            var units = AIModuleMgr.AllModules.SelectMany(p => p.EnterUnits).Where(p => p.Attr.Command == command).ToList();
            if (units.IsNullOrEmpty())
            {
                session.SendBack("未找到该功能！");
                return false;
            }

            var unit = units.First();
            if (units.Count > 1)
            {
                var options = units.Select(p => $"{p.Attr.Command} {p.Attr.SyntaxHint}").ToArray();
                var response = WaiterSvc.WaitForOptions(session, "请选择需要奖励的功能：", options);
                if (response < 0)
                {
                    session.SendBack("操作取消！");
                    return false;
                }

                unit = units[response];
            }

            var dailyLimit = DailyLimitDA.Get(aimUser.ID, unit.Attr.ID);
            dailyLimit.Decache(count);
            dailyLimit.Update();

            session.SendBack("奖励已生效！");
            return true;
        }

        [ModuleEnter(Command = "图片审核", Description = "审核一张图片", AuthLvl = AuthorityLevel.Developer)]
        public bool PicReview(InboxSession session)
        {
            PicReviewSvc.Review(session);
            return true;
        }

        [ModuleEnter(Command = "刷新数据 数据刷新", Description = "刷新所有数据信息", AuthLvl = AuthorityLevel.Developer)]
        public bool DataRefresh(InboxSession session)
        {
            var count = DataRefreshSvc.RefreshAll();
            session.SendBack($"刷新成功！共刷新 {count}个数据项！");
            return true;
        }

        [ModuleEnter(Command = "物品奖励", Description = "奖励某个人若干个物品", SyntaxHint = "[@用户名] [物品名] [物品数量]", AuthLvl = AuthorityLevel.Developer)]
        public bool ItemBonus(InboxSession session, InstanceUser aimUser, string name, int count)
        {
            var item = HonorSvc.FindItem(name);
            if (item == null)
            {
                session.SendBack("未找到该物品！");
                return false;
            }

            var record = ItemCollectionDA.Get(aimUser.ID);
            var msg = record.ItemIncome(name, count);
            if (!string.IsNullOrEmpty(msg))
            {
                session.SendBack(msg);
            }

            session.SendBack("奖励已生效！");
            return true;
        }

        [ModuleEnter(Command = "金币奖励", Description = "奖励某个人一些金币", SyntaxHint = "[@用户名] [金币数量]", AuthLvl = AuthorityLevel.Developer)]
        public bool GoldBonus(InboxSession session, InstanceUser aimUser, int golds)
        {
            var osPerson = OSPersonDA.GetPerson(aimUser.ID);
            osPerson.Golds += golds;
            osPerson.Update();

            session.SendBack("奖励已生效！");
            return true;
        }

        [ModuleEnter(Command = "钻石奖励", Description = "奖励某个人一些钻石", SyntaxHint = "[@用户名] [钻石数量]", AuthLvl = AuthorityLevel.Developer)]
        public bool DiamondBonus(InboxSession session, InstanceUser aimUser, int diamonds)
        {
            var osPerson = OSPersonDA.GetPerson(aimUser.ID);
            osPerson.Diamonds += diamonds;
            osPerson.Update();

            session.SendBack("奖励已生效！");
            return true;
        }

        [ModuleEnter(Command = "礼物奖励", Description = "奖励某个人若干件礼物", SyntaxHint = "[@用户名] [礼物名称] [礼物数量]", AuthLvl = AuthorityLevel.Developer)]
        public bool GiftBonus(InboxSession session, InstanceUser aimUser, string name, int count)
        {
            var gift = GiftSvc[name];
            if (gift == null)
            {
                session.SendBack("未找到该礼物！");
                return false;
            }

            var osPerson = OSPersonDA.GetPerson(aimUser.ID);
            osPerson.GiftIncome(name, count);
            osPerson.Update();

            session.SendBack("奖励已生效！");
            return true;
        }

        [ModuleEnter(Command = "宠物经验值奖励", Description = "奖励某个人若干宠物经验值", SyntaxHint = "[@用户名] [经验值]", AuthLvl = AuthorityLevel.Developer)]
        public bool PetExpBonus(InboxSession session, InstanceUser aimUser, int exp)
        {
            var pet = PetRecordDA.Get(aimUser.ID);
            var msg = pet.ExtGain(exp);
            msg += "\n奖励已生效！";

            session.SendBack(msg);
            return true;
        }

        [ModuleEnter(Command = "抽奖奖励", Description = "奖励某个人一些抽奖机会", SyntaxHint = "[@用户名] [奖励次数]", AuthLvl = AuthorityLevel.Developer)]
        public bool BonusChance(InboxSession session, InstanceUser aimUser, int count)
        {
            var personCache = PersonCacheRecordDA.Get(aimUser.ID, "抽奖");
            personCache.Value = (int.TryParse(personCache.Value, out var times) ? times + count : count).ToString();
            personCache.Update();

            session.SendBack("奖励已生效！");
            return true;
        }
    }
}